package com.chlitina.store.modules.cash.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.cash.model.Member;

/**
 * Crash Controller
 * 
 * @author Songsong.xu
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/cash/member/repayment")
public class RepaymentController extends BaseController {
	public static interface KEY {
		String KEY_MEMBER = "member";
		String KEY_TAB_ACTIVE = "tabActive";
	}

	public static interface URI {
		String URI_MEMBER_REPAYMENT = "modules/cash/repayment";
	}
 
	@RequestMapping(value = { "debt" })
	public String debt(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Member member = (Member) request.getSession().getAttribute(
				KEY.KEY_MEMBER);
		if(member.getRequiredRefundDebt()==member.getRefundDebt()){
			model.addAttribute("message", "错误的还款请求");
			return "modules/cash/cashMemberDetail";
		}
		return URI.URI_MEMBER_REPAYMENT;
	}
	
	@RequestMapping(value = { "repaydebt" })
	public String payDebt(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Member member = (Member) request.getSession().getAttribute(
				KEY.KEY_MEMBER);
		if(member.getRequiredRefundDebt()==member.getRefundDebt()){
			model.addAttribute("message", "错误的还款请求");
			return "modules/cash/cashMemberDetail";
		}
		String parameter = request.getParameter("refundDebt");
		float refundDebt = Float.parseFloat(parameter);
		if(refundDebt!=member.getRequiredRefundDebt()){
			model.addAttribute("message", "错误的还款金额，必须一次性还清全部欠款");
			return debt(request, response, model);
		}
		member.setRefundDebt(refundDebt);
		member.updateMenuState();
		model.addAttribute("message", "还款请求已计入结算列表");
		return "modules/cash/cashMemberDetail";
	}
}
